package arch.dao.impl.fs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;

import arch.domain.fs.Container;
import arch.domain.fs.File;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
public class EagerFileRowMapper implements RowMapper {

	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		File file = new File();
		
		file.setId(rs.getString("f_id"));
		file.setNamespace(rs.getString("f_namespace"));
		file.setName(rs.getString("f_name"));
		file.setOriginName(rs.getString("f_origin_name"));
		file.setSize(rs.getLong("f_size"));
		file.setCreator(rs.getString("f_creator"));
		java.sql.Timestamp createTime = rs.getTimestamp("f_create_time");
		java.sql.Timestamp lastUpdateTime = rs.getTimestamp("f_last_update_time");
		if(createTime != null) {
			file.setCreateTime(new Date(createTime.getTime()));
		}
		if(lastUpdateTime != null) {
			file.setLastUpdateTime(new Date(lastUpdateTime.getTime()));
		}
		file.setDescription(rs.getString("f_description"));
		
		Integer containerId = rs.getInt("f_container_id");
		if(containerId != null && containerId > 0) {
			Container container = new Container();
			container.setId(containerId);
			container.setPath(rs.getString("c_path"));
			container.setCapacity(rs.getLong("c_capacity"));
			container.setFree(rs.getLong("c_free"));
			container.setEnableString(rs.getString("c_enable"));
			container.setDescription(rs.getString("c_description"));
			
			file.setContainer(container);
		}
		
		return file;
	}

}
